package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.RegionQueryObject;
import cn.wolfcode.luowowo.repository.DestinationRepository;
import cn.wolfcode.luowowo.repository.RegionRepository;
import cn.wolfcode.luowowo.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService{

    @Autowired
    private RegionRepository regionRepository;

    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IRegionService regionService;


    @Override
    public Page<Region> query(RegionQueryObject qo) {
        /*Query query = new Query();
        return DBHelper.query(template,Region.class,query,qo.getPageable());*/

        //条件
        Query query = new Query();
        //PageResult
        //查询总记录数- totalCount
        //sql: select count(id) from table where ....
        long count = template.count(query, Region.class);
        if(count == 0){
            return Page.empty();
        }
        //页面显示数据集合-list
        //sql: select * from table where .... limit ....
        Pageable pageable = PageRequest.of( qo. getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC,"_id");
        query.with(pageable);
        List<Region> list = template.find(query, Region.class);

        return new PageImpl<Region>(list,pageable,count);

    }

    @Override
    public void save(Region region) {
        region.setId(null);
        regionRepository.save(region);
    }

    @Override
    public void update(Region region) {
        regionRepository.save(region);
    }

    @Override
    public Region findById(String rid) {
        return regionRepository.findById(rid).orElse(null);
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {

        // 1.通过 rid 获取关联的目的地 id 集合
        Region region = regionService.findById(rid);
        List<String> ids = region.getRefIds();
        // 2.通过 id 集合, 查询目的地对象集合
        List<Destination> list = destinationRepository.findByIdIn(ids);

        /*Region region = regionRepository.findById(rid).orElse(null);
        List<String> refIds = region.getRefIds();
        List<Destination> list = destinationRepository.findByIdIn(refIds);*/
        return list;
    }

    @Override
    public List<Destination> getDestByDeep(int deep) {
        List<Destination> destinations = destinationRepository.findAll();
        return destinations;
    }

    @Override
    public void deleteById(String id) {
        regionRepository.deleteById(id);
    }

    @Override
    public void saveOrUpdate(Region region) {
        if (StringUtils.hasLength(region.getId())) {
            //注意: mongodb的更新是全量更新，所以操作是应该先查 再替换最后更新
            //此处不需要: 需要修改参数个数刚好跟数据库的一样
            this.update(region);
        }else {
            this.save(region);
        }
    }

    @Override
    public void changeHotValue(String id, int hot) {
        Region region = this.findById(id);
        region.setIshot(hot);
        this.update(region);
    }
}
